#ifndef DAVMKCOL_H
#define DAVMKCOL_H
//

#include "base_modul.h"
#include "qxml.h"
#include "basedav.h"
#include "networkdav.h"


class NetworkDav;
    
class DavMKCOL : public QObject, public Base_Modul
{
    Q_OBJECT
//
public: 
    DavMKCOL( NetworkDav * db , const QString path )
{
    loopnummer = -1;
    pathregister = path;
    actualurl = path;
    davinfo = new QHttp();
    davinfo->setHost(db->Host(),db->Port());
    davinfo->setUser(db->User(),db->Pass());
    QHttpRequestHeader header("MKCOL", actualurl ,1,1);   /* header */
    header.setValue("Host", db->Host());
    header.setValue("User-Agent", WEBAGENTNAME );
    header.setValue("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    header.setValue("Accept-Language", "it,de-de;q=0.8,it-it;q=0.6,en-us;q=0.4,en;q=0.2");
    header.setValue("Connection", "keep-alive");
    david = davinfo->request(header,0,0);
     //////////////// connect(davinfo, SIGNAL(dataSendProgress(int, int)), this, SLOT(SendStatus(int, int)));
      /////////////////connect(davinfo, SIGNAL(done(bool)), this, SLOT(DavWake(bool)));
      connect(davinfo, SIGNAL(requestFinished(int, bool)), this, SLOT(Wake_Finish(int, bool)));
      ///////////connect(davinfo, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(RegisterBackHeader(const QHttpResponseHeader &)));
     
        
    
}
void  SetPosition( int position )
{
  loopnummer =   position;  
}
protected:
 void DavClose()
{
  
}
private:
  QString pathinfofile;
  QHttp *davinfo;
  int david;
  int loopnummer;
  int statuscode;
  QString actualurl;
  QString pathregister;
  QString xmlinfo;
  QString dummypath;
  QString DisplayName;
signals:
  void WarningMsg( QString e );
  void RemoteMkDir( int next , QString msg ); 
public slots:

void  Wake_Finish( int requestId, bool error )
{
    //////////////////qDebug() << "#### Wake_Finish...  ->" << wakeaborter; 
    
   if (requestId != david ) {
     return;
   }
   
    QHttpResponseHeader lastheader = davinfo->lastResponse();
    statuscode = lastheader.statusCode();
   
    davinfo->close();
   
   
    if (error) {
    emit WarningMsg(tr("Error on url: %1\nErrorMsg %2").arg(actualurl).arg(davinfo->errorString()));
    return;
    } 
    qDebug() << "#### HEADER STATUS CODE  ->" << statuscode; 
    
    
    
     QString MessageCase;
    bool sucessm = false;
    /*
    Status Codes
   Responses from a MKCOL request MUST NOT be cached as MKCOL has non-idempotent semantics.
    */
    
    switch(statuscode)
		{
		case 201:
		sucessm = true;
		      MessageCase = "201 (Created) - The collection or structured resource was created in its entirety.";
		break;
		case 403:
	          MessageCase = "403 (Forbidden) - This indicates at least one of two conditions: 1) the server does not allow the creation of collections at the given location in its namespace, or 2) the parent collection of the Request-URI exists but cannot accept members.";
                break;
		case 405:
                MessageCase = "405 (Method Not Allowed) - MKCOL can only be executed on a deleted/non-existent resource.";
		break;
		case 409:
                MessageCase = "409 (Conflict) - A collection cannot be made at the Request-URI until one or more intermediate collections have been created.";
 		break;
		case 415:
                MessageCase = "415 (Unsupported Media Type)- The server does not support the request type of the body.";
		break;
		case 507:
                MessageCase = "507 (Insufficient Storage) - The resource does not have sufficient space to record the state of the resource after the execution of this method";
 		break;
		default:
		        MessageCase = QString("%1 Unknow status code.....").arg(statuscode);
		break;
		
         }
    
         /* QString */
         MessageCase.prepend("\n");
         MessageCase.prepend(QString("Report from new collection: %1").arg(actualurl)); 
         
         
         
         
    if (statuscode != 403 ) {
         
                if (statuscode == 201) {
                  MessageCase = "";  
                }
    
            if (loopnummer == 0 or loopnummer > 0) {
                        int next = loopnummer + 1;
                        emit RemoteMkDir(next,MessageCase);
                        loopnummer = -1;       
                        return;
            }
    
    
    
    
    
    

    } else {
    emit WarningMsg(tr("Error on url make collection: %1\nErrorMsg %2\nHeader Status code %3\nMessage %4").arg(actualurl).arg(davinfo->errorString()).arg(statuscode).arg(MessageCase)); 
    }
}

};

	
	
	



//
#endif // DAVMKCOL_H

